<%@ include file="../include/taglib.jsp" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!-- Modal -->
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title">
        <c:choose>
            <c:when test="${not empty sysUser.id}">修改记录</c:when>
            <c:otherwise>增加记录</c:otherwise>
        </c:choose>
    </h4>
</div>
<div class="modal-body">
    <form id="editForm" class="form-horizontal style-form">
        <input type="hidden" value="${sysUser.id}" name="id"/>
        <div class="form-group">
            <label class="col-sm-2 control-label">登录账号</label>
            <div class="col-sm-6">
                <input placeholder="3~10位数字，字母，下划线的组合..." class="form-control" type="text" name="loginName"
                       value="${sysUser.loginName}"
                       <c:if test="${sysUser.id !=null}">readonly</c:if> maxlength="11"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">真实姓名</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" name="realName" value="${sysUser.realName}" maxlength="11"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">邮箱</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" name="email" value="${sysUser.email}" maxlength="127"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">电话</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" name="telephone" value="${sysUser.telephone}" maxlength="11"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">部门</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" name="department" value="${sysUser.department}" maxlength="11"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">用户等级</label>
            <div class="col-sm-3">
                <select name="level" class="form-control">
                    <c:forEach var="i" begin="${loginUser.level+1}" end="5">
                        <option value="${i}" <c:if test="${sysUser.level == i}">selected</c:if>>
                            <b>
                                <c:choose>
                                    <c:when test="${i == 0}">①级</c:when>
                                    <c:when test="${i == 1}">②级</c:when>
                                    <c:when test="${i == 2}">③级</c:when>
                                    <c:when test="${i == 3}">④级</c:when>
                                    <c:when test="${i == 4}">⑤级</c:when>
                                    <c:when test="${i == 5}">⑥级</c:when>
                                </c:choose>
                            </b>
                        </option>
                    </c:forEach>
                </select>
            </div>
        </div>
    </form>
</div>
<div class="modal-footer">
    <button data-dismiss="modal" class="btn btn-default" type="button" id="cancelBtn">取消</button>
    <button class="btn btn-theme" type="button" id="saveBtn">保存</button>
</div>
<!-- modal -->
<script type="text/javascript">
    $(function () {
        $("#saveBtn").bind("click", function () {
            if (!checkForm("#editForm")) {
                return;
            }
            var data = $("#editForm").serializeArray();
            var package = new AJAXPacket("${ctx}/sysuser/save");
            package.data.data = data;
            core.ajax.sendPacket(package, function (response) {
                if (response.data.success) {
                    alertMsg.correct("成功！");
                    $("#cancelBtn").click();
                    jumpPageTo('_list');
                } else {
                    alertMsg.error(response.data.errMsg);
                }
            });
            package = null;
        });
    });

    function checkForm(obj) {
        var patten = new RegExp(/^[a-zA-Z0-9_]{3,10}$/);
        if (!patten.test($("input[name=loginName]").val())) {
            alertMsg.warn("登录账号输入格式不正确！");
            $("input[name=loginName]").focus();
            return false;
        }
        if ("" == $.trim($("input[name=realName]").val())) {
            alertMsg.warn("真实姓名不能为空！");
            $("input[name=name]").focus();
            return false;
        }
        var email = $.trim($("input[name=email]").val());
        if ("" != email) {
            var patten = new RegExp(/^(.+)@(.+)$/);
            if (!patten.test(email)) {
                alertMsg.warn("邮箱输入格式不正确！");
                $(obj).find("input[name=email]").focus();
                return false;
            }
        }
        var telephone = $.trim($("input[name=telephone]").val());
        if ("" != telephone) {
            var patten = new RegExp(/^[0-9]{11}$/);
            if (!patten.test(telephone)) {
                alertMsg.warn("号码输入格式不正确！");
                $(obj).find("input[name=telephone]").focus();
                return false;
            }
        }
        return true;
    }
</script>